package com.iwop.moto.service.impl;

import com.iwop.moto.model.SMS;
import com.iwop.moto.model.User;
import com.iwop.moto.service.BaseService;
import com.iwop.moto.service.SMSService;
import com.iwop.moto.service.UserService;
import com.iwop.moto.utils.stringUtil.StringUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Created by huyuangui on 2016/7/7.
 */
@Service
public class SMSServiceImpl extends BaseService implements SMSService {
    public static  String findByTelOrId="com.iwop.moto.dao.SMSMapper.findByTelOrId";
    public static  String getLastSMS="com.iwop.moto.dao.SMSMapper.getLastSMS";
    public static  String update="com.iwop.moto.dao.SMSMapper.update";
    public static  String insert="com.iwop.moto.dao.SMSMapper.insert";
    public static  String findCheckCodeByTel="com.iwop.moto.dao.SMSMapper.findCheckCodeByTel";
    @Override
    public SMS findByTelOrId(SMS sms) {
        if(sms!=null && StringUtils.isNotEmpty(sms.getTel())) {
            Map<String,Object> map=new HashMap<String,Object>();
            map.put("tel",sms.getTel());
            map.put("sid",sms.getSid());
            SMS smsTemp= getSqlSession().selectOne(findByTelOrId, map);
            return  smsTemp;
        }
        return null;
    }

    @Override
    public SMS save(SMS sms) {
        if(sms!=null){
            if(sms.getSid()>0){
                getSqlSession().update(update,sms);
                return findByTelOrId(sms);
            }else{
                getSqlSession().insert(insert,sms);
                return getLastSMS();
            }
        }
        return null;
    }
    @Override
    public SMS getLastSMS(){
        SMS smsTemp= getSqlSession().selectOne(getLastSMS, null);
        return  smsTemp;
    }

    @Override
    public SMS findCheckCodeByTel(SMS sms){
        return  getSqlSession().selectOne(findCheckCodeByTel, sms);
    }
}